package com.codemeta.bokit.oss.config;

import cn.xuyanwu.spring.file.storage.EnableFileStorage;
import cn.xuyanwu.spring.file.storage.FileStorageAutoConfiguration;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import cn.xuyanwu.spring.file.storage.platform.FileStorage;
import cn.xuyanwu.spring.file.storage.platform.LocalFileStorage;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.context.annotation.Configuration;

/**
 * 存储组件自动装配
 *
 * @author hice
 * @date 2021/11/18
 */
@Slf4j
@Configuration
@EnableFileStorage
@AutoConfigureAfter(FileStorageAutoConfiguration.class)
public class OssAutoConfig {

    private final static String LOCAL_DEFAULT = "local-default";

    public OssAutoConfig(FileStorageService service) {
        // 获得存储平台 List
        CopyOnWriteArrayList<FileStorage> list = service.getFileStorageList();
        if (list.isEmpty()) {
            // 存储平台为空时增加默认本地存储
            LocalFileStorage local = new LocalFileStorage();
            local.setPlatform(LOCAL_DEFAULT);
            local.setBasePath("../temp/");
            list.add(local);
        }
        String plats = service.getFileStorageList().stream().map(FileStorage::getPlatform)
            .collect(Collectors.joining(","));
        log.debug("【bokit】注入FileStorage组件,已配置平台：{}", plats);
    }
}